Micron Document
Livres et Wikis | Archives | Info


Interface (object-oriented programming)
part 3/4 Β· 9.9 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Type classes in languages like Haskell, or module signatures in ML and OCaml, are used for many of the things that interfaces are used for.

In Rust, interfaces are called traits.cite-ref-5[4] In Rust, structs do not have methods, but instead implement traits which declare methods that the struct implements.

trait Pet {
fn speak(&self);
}
struct Dog<'a> {
name: &'a str
}
impl<'a> Dog<'a> {
fn new(name: &'a str) -> Self {
Dog { name }
}
}
impl Pet<'a> for Dog<'a> {
fn speak(&self) {
println!("{} says 'Woof!'", self.name);
}
}

See also



──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────